package com.rongmai.web.ctrl;

import com.rongmai.db.model.BaseResult;
import com.rongmai.db.model.FinanceProductAudit;
import com.rongmai.web.bo.FinanceProductBo;
import com.rongmai.web.utils.UserUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@RequestMapping("/financeproduct")
public class FinanceProductCtrl {

    @Resource
    private FinanceProductBo financeProductBo;

    @RequestMapping(value = "add", method = RequestMethod.POST)
    public BaseResult add(@RequestBody FinanceProductAudit financeProduct, HttpServletRequest request) {
        if (financeProduct == null || StringUtils.isEmpty(financeProduct.getName())) {
            return BaseResult.fail("参数非法");
        }
        try {
            financeProduct.setCreateBy(UserUtils.getCurrentUserName(request));
            financeProductBo.add(financeProduct);
            return BaseResult.succ();
        } catch (Exception e) {
            log.error("操作异常", e);
            return BaseResult.fail("操作异常");
        }
    }

    @RequestMapping(value = "edit", method = RequestMethod.POST)
    public BaseResult edit(@RequestBody FinanceProductAudit financeProduct) {
        if (financeProduct == null || StringUtils.isEmpty(financeProduct.getName())) {
            return BaseResult.fail("参数非法");
        }
        try {
            financeProductBo.edit(financeProduct);
            return BaseResult.succ();
        } catch (Exception e) {
            log.error("操作异常", e);
            return BaseResult.fail("操作异常");
        }
    }

    @RequestMapping(value = "delete", method = RequestMethod.GET)
    public BaseResult delete(Long id) {
        if (id == null) {
            return BaseResult.fail("参数非法");
        }
        try {
            financeProductBo.delete(id);
            return BaseResult.succ();
        } catch (Exception e) {
            log.error("操作异常", e);
            return BaseResult.fail("操作异常");
        }
    }

    @RequestMapping(value = "get", method = RequestMethod.GET)
    public BaseResult get(Long id) {
        if (id == null) {
            return BaseResult.fail("参数非法");
        }
        try {
            return BaseResult.succ(financeProductBo.get(id));
        } catch (Exception e) {
            log.error("操作异常", e);
            return BaseResult.fail("操作异常");
        }
    }
}
